package com.baosight.sczx.zm.cd.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cd.Documents;
import com.baosight.sczx.zm.cd.domain.ZMCDDirectory;

/**
 * <p>Title: ServiceZMCD11.java</p>
 * <p>Description: 目录管理</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2011-8-24 下午05:08:12  张志芳  Create
 *</pre>
 */
public class ServiceZMCD11 extends ServiceEPBase {
	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP70</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6下午03:53:52 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
 
		inInfo.set("fdCreatorId", UserSession.getUserId());//登录人的默认值
		inInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
		inInfo.set("fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));//单位的默认值		
		inInfo.set("fdCreateDepartment", UserSession.getInSessionProperty("orgName"));//单位的默认值
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		inInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cd.department", "orgCode", "orgName", false));//部门
		inInfo.addBlock(StaticBlocks.GetLimit());
		inInfo.addBlock(CDBlocks.getDirBlock());
		EiBlock block = inInfo.addBlock("cmes.cd.department1");
		block.addMeta(new EiColumn("orgCode"));
		block.addMeta(new EiColumn("orgName"));
		Map<String,String> map = new HashMap<String,String>();
		map.put("fdDirId", inInfo.getCellStr(EiConstant.queryBlock, 0, "fdDirId"));
		block.addRows(dao.query("ZMCDDirLimtOrg.queryNotLimitOrg", map));
		return inInfo;
	}
	
	/**
	 * 查询文档目录记录
	 * <p>Function: query1</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-16下午12:43:17 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		StaticTools.setQueryParameters(inInfo, "fdSuperDirId", inInfo.getString("fdSuperDirId"), true);//添加查询条件
		EiInfo outInfo = super.query(inInfo, "ZMCDDirectory.query", new ZMCDDirectory());
		outInfo.addBlock(CDBlocks.getDirBlock());
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cd.department", "orgCode", "orgName", false));//部门
		return outInfo;
	}
	
	/**
	 * 目录树的新增和权限的新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCD01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-16下午12:44:31 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {		
		EiInfo out=new EiInfo(); 
		ZMCDDirectory _dom = new ZMCDDirectory();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));  
			String keyId = TablePk.getInstance().getNextValue("T_CD_DIRECTORY");
			_dom.setFdKeyId(keyId);
			_dom.setFdCreatorId(UserSession.getUserId().toString());
			_dom.setFdDepartmentId(UserSession.getInSessionProperty("orgCode").toString());
			this.dao.insert("ZMCDDirectory.insert",_dom);  
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;  
	}

	/**
	 * 目录树的修改和它权限的修改
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCD01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-16下午12:44:22 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCDDirectory _dom = new ZMCDDirectory();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));  
			this.dao.update("ZMCDDirectory.update",_dom);  
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;   
	}

	/**
	 * 目录树的删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCD01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-16下午12:44:06 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		super.delete(inInfo, "ZMCDDirLimtOrg.dirdelete");
		EiInfo _temp = super.delete(inInfo,"ZMCDDirectory.delete");
		
        EiInfo outInfo = query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;		
	}
	
	/**
	 * 删除前先判断是否有文档记录,是否有子目录
	 * <p>Function: isExistDoc</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-25下午12:56:30 张志芳
	 * </p>
	 *
	 */
	public EiInfo canDelete(EiInfo inInfo)
	{	
		String id = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdKeyId");
		//判断是否有子目录
		Map<String, String> map = new HashMap<String, String>();
		map.put("fdSuperDirId", id);
		List<?> list = dao.query("ZMCDDirectory.query", map);
		if(list.size()>0)
		{
			inInfo.setStatus(-1);
			inInfo.setMsg("该目录下有子目录，不能删除！");	
			return inInfo;
		}
		
		//判断是否有文档记录
		if(Documents.isExistDoc(id))
		{
			inInfo.setStatus(-1);
			inInfo.setMsg("该目录下有文档记录，不能删除！");	
			return inInfo;
		}
		return inInfo;
	}



	/**
	 * 校验目录是否重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-16下午12:48:34 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public EiInfo validate(EiInfo inInfo) {
		Map params = new HashMap();
		ZMCDDirectory _dom = new ZMCDDirectory();
		EiBlock eiBlock = inInfo.getBlock("result");
		String oper=(String)inInfo.get("efCurButtonEname");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));  
			params.put("fdSuperDirId", _dom.getFdSuperDirId());
			params.put("fdDirName",_dom.getFdDirName());
			if(oper.equalsIgnoreCase("update")){
				params.put("fdKeyId",_dom.getFdKeyId());
			}
			List result = dao.query("ZMCDDirectory.validate", params);
			if (result != null && result.size() > 0) {
				inInfo.setStatus(-1);
				inInfo.setMsg("目录名称已经存在，请更换目录名称！");

				return inInfo;
			}
		}
		inInfo.setStatus(1);
		return inInfo;
	}

}
